<?php
/*
 *      template.class.php
 *
 *      Copyright 2010 Patrice Ferlet <metal3d@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

// $Id: template.class.php 19 2010-12-14 22:46:23Z Metal3d $
// $HeadURL: http://valaya.googlecode.com/svn/trunk/utils/template.class.php $

/**
 *  Basic template object, it uses apc to create caches
 *
 * */
class Template {

	private $_vars = array();
	private $cache_name;
	private $cache_ttl;

	static $main;

	/**
	 * Construct Template object
	 *
	 * @param string $tpl
	 * */
	public function __construct($tpl) {
		if ( !($theme = _config('theme','project')) ) {
			$theme = 'default';
		}
		if( !file_exists(_config('base','project').'/'.$theme.'/'.$tpl) ) {
			$theme = 'default';
		}

		$this->tpl = _config('base','project').'templates/'.$theme.'/'.$tpl;
		
		//if themed file doesn't exists, set template to default
		if(!file_exists($this->tpl)) {
			$this->tpl = _config('modulepath','project').''.Project::$currentModule.'/templates/'.$tpl.'.php';
		}
	}

	/**
	 * Set apc_cache
	 *
	 * @param string $name
	 * @param int $ttl
	 * */
	public function setCache($name,$ttl=60) {
		$this->cache_name = $name;
		$this->cache_ttl  = $ttl;
	}


	/**
	 * Return main template for current theme
	 *
	 * @return Template $maintemplate
	 * */
	public function getMain(){
		if (is_null(self::$main)){
			self::$main = new self( _config('maintemplate','project') ?
			_config('maintemplate','project') :
						'main.php' );
		}
		return self::$main;

	}

	/**
	 * Return the processed content
	 *
	 * @return string $content
	 * */
	public function getContent() {
		if ( strlen($this->cache_name)>0 ) {
			if (( $content = apc_fetch($this->cache_name) ) === false ) {
				ob_start();
				include $this->tpl;
				$content = ob_get_clean();
				apc_store($this->cache_name,$content,$this->cache_ttl);
			}
			return $content;
		}
		ob_start();
		include $this->tpl;
		return ob_get_clean();
	}


	/**
	 * If cache is made, return the cache content either false
	 *
	 * @return string $name
	 * */
	public function getCache($name=false) {
		$name = ($name) ? $name : $this->cache_name;
		return ($r = apc_fetch($name)) ? $r : false;
	}


	public function __get($name) {
		$r = "";
		if(_config('debug','design') === true ) {
			$r = "-- $name not set -- ";
		}
		return isset($this->_vars[$name]) ? $this->_vars[$name] : $r;
	}

	public function __set($name,$val) {
		$this->_vars[$name]=$val;
	}


	public function __toString(){
		return $this->getContent();
	}
}
